package com.example.dlg;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class SingleSelectDialogActivity extends AppCompatActivity {

    public static final int SIGNAL_DIALOG = 0x113;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_select_dialog);

        Button btn6 = findViewById(R.id.btn6);
        btn6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog(SIGNAL_DIALOG);
            }
        });
    }

    @Nullable
    @Override
    protected Dialog onCreateDialog(int id, Bundle args) {
        // 弹出一个提醒对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(SingleSelectDialogActivity.this);
        switch (id){
            case SIGNAL_DIALOG:
                builder.setTitle("列表对话框");
                String[] colors = new String[]{"红色","黄色","蓝色"};
                builder.setSingleChoiceItems(colors,0,new SingleSelectDialogActivity.ItemOnClickListener());
                builder.setPositiveButton("确定",new SingleSelectDialogActivity.ItemOnClickListener());
                break;
        }
        AlertDialog alertDialog = builder.create();
        return alertDialog;
    }

    class ItemOnClickListener implements DialogInterface.OnClickListener{

        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case 0:
                    Toast.makeText(SingleSelectDialogActivity.this,"红",Toast.LENGTH_SHORT).show();
                    break;
                case 1:
                    Toast.makeText(SingleSelectDialogActivity.this,"黄",Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    Toast.makeText(SingleSelectDialogActivity.this,"蓝",Toast.LENGTH_SHORT).show();
                    break;
                default:
                    Toast.makeText(SingleSelectDialogActivity.this,"红",Toast.LENGTH_SHORT).show();
            }

        }
    }
}